package com.wannengbang.storemobile.homepage;


import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.gson.Gson;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.wannengbang.storemobile.R;
import com.wannengbang.storemobile.base.BaseFragment;
import com.wannengbang.storemobile.base.DataCallBack;
import com.wannengbang.storemobile.bean.BaseResponseBean;
import com.wannengbang.storemobile.bean.EBankStoreDataBasic;
import com.wannengbang.storemobile.bean.EBankStoreDataBean;
import com.wannengbang.storemobile.bean.EBankStoreDataContacts;
import com.wannengbang.storemobile.bean.EBankStoreDataInfo;
import com.wannengbang.storemobile.bean.ImageBean;
import com.wannengbang.storemobile.bean.StoreDeatilsBean;
import com.wannengbang.storemobile.event.ALiDiscernEvent;
import com.wannengbang.storemobile.event.SignBitmapEvent;
import com.wannengbang.storemobile.event.StoreDataEvent;
import com.wannengbang.storemobile.homepage.model.HomePageModelImpl;
import com.wannengbang.storemobile.homepage.model.IHomePageModel;
import com.wannengbang.storemobile.utils.Base64BitmapUtil;
import com.wannengbang.storemobile.utils.DateTimeUtil;
import com.wannengbang.storemobile.utils.GlideEngine;
import com.wannengbang.storemobile.utils.GlideUtils;
import com.wannengbang.storemobile.utils.PermissionsManager;
import com.wannengbang.storemobile.utils.SPManager;
import com.wannengbang.storemobile.utils.ToastUtil;
import com.wannengbang.storemobile.widget.ViewLoading;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;

/**
 * 上次资料
 */
public class EBankStoreUpDataFragment extends BaseFragment {
    public static final String TITLE_TYPE = "type";
    public static final String MERCHANT_ID = "merchant_id";
    public static final String QUICK_INCOME = "quick_income";
    @BindView(R.id.ic_prcture_1)
    ImageView icPrcture1;
    @BindView(R.id.iv_picture_demo_1)
    ImageView ivPictureDemo1;
    @BindView(R.id.ic_prcture_7)
    ImageView icPrcture7;
    @BindView(R.id.iv_picture_demo_7)
    ImageView ivPictureDemo7;
    @BindView(R.id.ll_hand_photo)
    LinearLayout llHandPhoto;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.ic_prcture_2)
    ImageView icPrcture2;
    @BindView(R.id.iv_picture_demo_2)
    ImageView ivPictureDemo2;
    @BindView(R.id.ic_prcture_3)
    ImageView icPrcture3;
    @BindView(R.id.iv_picture_demo_3)
    ImageView ivPictureDemo3;
    @BindView(R.id.ll_company_data)
    LinearLayout llCompanyData;
    @BindView(R.id.tv_sign)
    TextView tvSign;

    private String type;
    private static final int REQUEST_CODE_SIGN = 10001;

    private String prctureDemo1 = "https://wnb-dc.oss-cn-qingdao.aliyuncs.com/resources/73aa7ade644dce8bc6d103fc08480f5.jpg";
    private String prctureDemo2 = "https://wnb-dc.oss-cn-qingdao.aliyuncs.com/resources/201901121113373.jpg";
    private String prctureDemo7 = "https://wnb-dc.oss-cn-qingdao.aliyuncs.com/resources/shouchishenfenzheng.jpg";
    private String prctureDemo3 = "https://wnb-dc.oss-cn-qingdao.aliyuncs.com/resources/201901121113371.jpg";
    private BottomSheetDialog selectPicDialog;
    private PermissionsManager permissionsManager;
    private String[] permissions;
    private int openType;//0 相册，1相机
    private List<LocalMedia> mediaList = new ArrayList<>();
    //1 2 3 4 5 6 7 8  20  21 22
    private int imgTpye = 1;
    private String currentTime;
    private EBankStoreDataBasic storeDataBasic;
    private EBankStoreDataContacts storeDataCompany;
    private EBankStoreDataInfo storeDataInfo;
    private EBankStoreDataBean storeDataBean;
    private Gson mGson = new Gson();
    private IHomePageModel homePageModel = new HomePageModelImpl();
    private String imgPath1, imgPath2, imgPath3, imgPath7;

    private StoreDeatilsBean.DataBean storeBean;
    private String merchant_id;
    private int quick_income;
    private String sign_pic;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ebank_store_updata, container, false);
        ButterKnife.bind(this, view);
        registerEventBus();
        Bundle bundle = getArguments();
        if (bundle != null) {
            type = bundle.getString(TITLE_TYPE);
            StoreDataEvent dataEvent = EventBus.getDefault().getStickyEvent(StoreDataEvent.class);
            if (dataEvent != null) {
                storeBean = dataEvent.getDataBean();
            }
            merchant_id = bundle.getString(MERCHANT_ID);
            quick_income = bundle.getInt(QUICK_INCOME);
        }
        initView();
        initData();
        return view;
    }

    private void initView() {
        storeDataBean = new EBankStoreDataBean();
        llCompanyData.setVisibility(View.GONE);
        tvSign.setVisibility(View.GONE);
        if ("1".equals(type)) {

        } else if ("2".equals(type)) {

        } else if ("3".equals(type)) {
            llCompanyData.setVisibility(View.VISIBLE);
            tvSign.setVisibility(View.VISIBLE);
        }

    }


    public void initData() {
        permissionsManager = new PermissionsManager(mActivity);
        currentTime = DateTimeUtil.format(new Date());
        GlideUtils.loadImage(mActivity, ivPictureDemo1, prctureDemo1);
        GlideUtils.loadImage(mActivity, ivPictureDemo2, prctureDemo2);
        GlideUtils.loadImage(mActivity, ivPictureDemo3, prctureDemo3);
        GlideUtils.loadImage(mActivity, ivPictureDemo7, prctureDemo7);

        if (storeBean != null) {
            imgPath1 = storeBean.getDoor_pic();
            imgPath2 = storeBean.getInside_pic();
            imgPath3 = storeBean.getCashier_desk_pic();
            imgPath7 = storeBean.getLegal_id_card_hand_pic();
            if (!TextUtils.isEmpty(imgPath1)) {
                GlideUtils.loadImage(mActivity, icPrcture1, imgPath1);
            }
            if (!TextUtils.isEmpty(imgPath2)) {
                GlideUtils.loadImage(mActivity, icPrcture2, imgPath2);
            }
            if (!TextUtils.isEmpty(imgPath3)) {
                GlideUtils.loadImage(mActivity, icPrcture3, imgPath3);
            }
            if (!TextUtils.isEmpty(imgPath7)) {
                GlideUtils.loadImage(mActivity, icPrcture7, imgPath7);
            }
        }
    }


    @Subscribe
    public void onSignBitmapEvent(SignBitmapEvent event) {
        /**
         * 签名
         */
        sign_pic = event.getBitmapProtocolurl();
        storeDataBean.setSign_pic(sign_pic);
    }

    @Subscribe
    public void onALiDiscernEvent(ALiDiscernEvent event) {

    }

    private boolean isViewCreated; // 界面是否已创建完成
    private boolean isVisibleToUser; // 是否对用户可见

    /**
     * Fragment 创建期间setUserVisibleHint()方法是在onActivityCreated()之前被调用
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        tryLoadData();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewCreated = true;
        tryLoadData();
    }

    public void tryLoadData() {
        if (isViewCreated && isVisibleToUser) {
            if (!TextUtils.isEmpty(SPManager.getInstance().getStoreBasic())) {
                storeDataBasic = mGson.fromJson(SPManager.getInstance().getStoreBasic(), EBankStoreDataBasic.class);
                if (storeDataBasic != null) {
                    tvSign.setVisibility(View.GONE);
                    if ("0".equals(storeDataBasic.getMerchant_type())) {
                        type = "1";
                        llCompanyData.setVisibility(View.GONE);
                    } else if ("1".equals(storeDataBasic.getMerchant_type())) {

                    } else if ("3".equals(storeDataBasic.getMerchant_type())) {
                        type = "3";
                        llCompanyData.setVisibility(View.VISIBLE);
                        tvSign.setVisibility(View.VISIBLE);
                    }
                }
            }
            if (!TextUtils.isEmpty(SPManager.getInstance().getStoreCompany())) {
                storeDataCompany = mGson.fromJson(SPManager.getInstance().getStoreCompany(), EBankStoreDataContacts.class);
            }
            if (!TextUtils.isEmpty(SPManager.getInstance().getStoreInfo())) {
                storeDataInfo = mGson.fromJson(SPManager.getInstance().getStoreInfo(), EBankStoreDataInfo.class);
            }
        }
    }


    @OnClick({R.id.ic_prcture_1, R.id.ic_prcture_2, R.id.ic_prcture_3, R.id.ic_prcture_7, R.id.tv_sign, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ic_prcture_1:
                imgTpye = 1;
                showPictureSelectorDialog();
                break;
            case R.id.ic_prcture_2:
                imgTpye = 2;
                showPictureSelectorDialog();
                break;
            case R.id.ic_prcture_3:
                imgTpye = 3;
                showPictureSelectorDialog();
                break;
            case R.id.ic_prcture_7:
                imgTpye = 7;
                showPictureSelectorDialog();
                break;
            case R.id.tv_sign:
                Intent intent = new Intent(mActivity, PenActivity.class);
                intent.putExtra("type", "ebank");
                mActivity.startActivity(intent);
                break;
            case R.id.tv_commit:
                commit();
                break;
        }
    }

    public void commit() {

        if (storeDataBasic == null) {
            ToastUtil.showShort("请填写门店资料基本信息");
            return;
        }
        if (storeDataCompany == null) {
            ToastUtil.showShort("请填写门店资料联系人信息");
            return;
        }
        if (storeDataInfo == null) {
            ToastUtil.showShort("请填写门店资料银行信息");
            return;
        }
        if (TextUtils.isEmpty(storeDataBasic.getMcc_code())) {
            ToastUtil.showShort("请选择行业类型");
            return;
        }
        if ("3".equals(type)) {
            if (TextUtils.isEmpty(storeDataBasic.getLicense_pic())) {
                ToastUtil.showShort("请上传营业执照");
                return;
            }
            if (TextUtils.isEmpty(storeDataBasic.getLicense_no())) {
                ToastUtil.showShort("请输入营业执照编号");
                return;
            }
            if (TextUtils.isEmpty(storeDataBasic.getLicense_full_name())) {
                ToastUtil.showShort("请输入营业执照名称");
                return;
            }
            if (TextUtils.isEmpty(storeDataBasic.getLicense_address())) {
                ToastUtil.showShort("请输入营业执照地址");
                return;
            }
            if (TextUtils.isEmpty(storeDataBasic.getLicense_start())) {
                ToastUtil.showShort("请输入营业执照开始时间");
                return;
            }
            if (!storeDataBasic.isLongTime() && TextUtils.isEmpty(storeDataBasic.getLicense_end())) {
                ToastUtil.showShort("请输入营业执照结束时间");
                return;
            }
            if (TextUtils.isEmpty(imgPath2)) {
                ToastUtil.showShort("请上传门店店内照片");
                return;
            }
            if (TextUtils.isEmpty(imgPath3)) {
                ToastUtil.showShort("请上传门店收银台照");
                return;
            }
        }
        if (TextUtils.isEmpty(storeDataBasic.getMerchant_name())) {
            ToastUtil.showShort("请输入门店名称");
            return;
        }
        if (TextUtils.isEmpty(storeDataBasic.getProvince())) {
            ToastUtil.showShort("请选择省市县区");
            return;
        }
        if (TextUtils.isEmpty(storeDataBasic.getAddress())) {
            ToastUtil.showShort("请输入详细地址");
            return;
        }
        if (TextUtils.isEmpty(storeDataBasic.getLat()) || "null".equals(storeDataBasic.getLat())) {
            ToastUtil.showShort("请选择位置");
            return;
        }
        if (TextUtils.isEmpty(storeDataBasic.getLng()) || "null".equals(storeDataBasic.getLng())) {
            ToastUtil.showShort("请选择位置");
            return;
        }
        if (TextUtils.isEmpty(storeDataCompany.getLegal_id_card_front_pic())) {
            ToastUtil.showShort("请上传法人身份证正面");
            return;
        }

        if (TextUtils.isEmpty(storeDataCompany.getLegal_id_card_back_pic())) {
            ToastUtil.showShort("请上传法人身份证反面");
            return;
        }

        if (TextUtils.isEmpty(storeDataCompany.getContact_name())) {
            ToastUtil.showShort("请输入联系人姓名");
            return;
        }

        if (TextUtils.isEmpty(storeDataCompany.getLegal_id_card_no())) {
            ToastUtil.showShort("请输入联系人身份证编号");
            return;
        }

        if (TextUtils.isEmpty(storeDataCompany.getLegal_id_card_start())) {
            ToastUtil.showShort("请选择联系人身份证开始时间");
            return;
        }
        if (!storeDataCompany.isLongTime() && TextUtils.isEmpty(storeDataCompany.getLegal_id_card_end())) {
            ToastUtil.showShort("请选择联系人身份证结束时间");
            return;
        }

        if (TextUtils.isEmpty(storeDataInfo.getBank_card_front_pic())) {
            ToastUtil.showShort("请上传银行卡号面照片");
            return;
        }

        if (TextUtils.isEmpty(storeDataInfo.getBranch())) {
            ToastUtil.showShort("请选择开户行名称");
            return;
        }
        if (TextUtils.isEmpty(storeDataInfo.getHolder())) {
            ToastUtil.showShort("请输入开户名");
            return;
        }
        if (TextUtils.isEmpty(storeDataInfo.getHolder_mobile())) {
            ToastUtil.showShort("请输入开户预留电话");
            return;
        }
        if (TextUtils.isEmpty(storeDataInfo.getHolder_id_card_no())) {
            ToastUtil.showShort("请输入开户人身份证号");
            return;
        }
        if (TextUtils.isEmpty(storeDataInfo.getBank_card_no())) {
            ToastUtil.showShort("请输入银行卡号");
            return;
        }

        if (TextUtils.isEmpty(imgPath1)) {
            ToastUtil.showShort("请上传门店门头照");
            return;
        }
        if (TextUtils.isEmpty(imgPath7)) {
            ToastUtil.showShort("请上传手持身份证照");
            return;
        }
        if (TextUtils.isEmpty(imgPath2)) {
            ToastUtil.showShort("请上传门店店内照片");
            return;
        }
        if ("3".equals(type)) {
            if (TextUtils.isEmpty(imgPath3)) {
                ToastUtil.showShort("请上传门店收银台照");
                return;
            }
            if (TextUtils.isEmpty(sign_pic)) {
                ToastUtil.showShort("请上传电子签名");
                return;
            }
        }
        storeDataBean.setDoor_pic(imgPath1);
        storeDataBean.setInside_pic(imgPath2);
        storeDataBean.setCashier_desk_pic(imgPath3);
        storeDataBean.setLegal_id_card_hand_pic(imgPath7);

        showProgress("上传中.....");
        saveProfile();

    }

    /**
     * 保存资料
     */
    public void saveProfile() {
        if (storeDataBasic == null) {
            return;
        }
        if (storeDataCompany == null) {
            return;
        }
        if (storeDataInfo == null) {
            return;
        }

        if (storeBean != null) {
            homePageModel.requestEBankStoreSaveProfile(
                    storeBean.getStore_no(), storeDataBasic.getMerchant_type(), storeDataBasic.getMerchant_name(),
                    storeDataBasic.getLicense_no(), storeDataBasic.getLicense_full_name(), storeDataBasic.getLicense_address(), storeDataBasic.getLicense_start(), storeDataBasic.getLicense_end(), storeDataBasic.getLicense_pic(),
                    storeDataBasic.getProvince(),
                    storeDataBasic.getCity(), storeDataBasic.getArea(), storeDataBasic.getAddress(), storeDataBasic.getLng(), storeDataBasic.getLat(),
                    storeDataCompany.getContact_name(),
                    storeDataInfo.getHolder(), storeDataCompany.getLegal_id_card_no(),
                    storeDataCompany.getLegal_id_card_start(), storeDataCompany.getLegal_id_card_end(), storeDataInfo.getBank(), storeDataInfo.getBranch(),
                    storeDataInfo.getBranch_province(), storeDataInfo.getBranch_city(), storeDataInfo.getBank_card_no(), storeDataInfo.getHolder_mobile(), storeDataInfo.getUnionpay(), storeDataBean.getDoor_pic(),
                    storeDataInfo.getBank_card_front_pic(), storeDataCompany.getLegal_id_card_front_pic(),
                    storeDataCompany.getLegal_id_card_back_pic(), storeDataBean.getLegal_id_card_hand_pic(), storeDataBean.getInside_pic(), storeDataBean.getCashier_desk_pic(),
                    storeDataBean.getSign_pic(), storeDataBasic.getMcc_code(), new DataCallBack<BaseResponseBean>() {
                        @Override
                        public void onSuccessful(BaseResponseBean bean) {
                            dismissProgress();
                            ToastUtil.showShort(bean.getMsg());
                            mActivity.finish();
                        }

                        @Override
                        public void onFailed(String msg, String code) {
                            dismissProgress();
                        }
                    });
        } else {
            homePageModel.requestEBankStoreIncomeProfile(storeDataBasic.getMerchant_type(), storeDataBasic.getMerchant_name(),
                    storeDataBasic.getLicense_no(), storeDataBasic.getLicense_full_name(), storeDataBasic.getLicense_address(), storeDataBasic.getLicense_start(), storeDataBasic.getLicense_end(), storeDataBasic.getLicense_pic(),
                    storeDataBasic.getProvince(),
                    storeDataBasic.getCity(), storeDataBasic.getArea(), storeDataBasic.getAddress(), storeDataBasic.getLng(), storeDataBasic.getLat(), storeDataCompany.getContact_name(),
                    storeDataInfo.getHolder(), storeDataCompany.getLegal_id_card_no(),
                    storeDataCompany.getLegal_id_card_start(), storeDataCompany.getLegal_id_card_end(), storeDataInfo.getBank(), storeDataInfo.getBranch(),
                    storeDataInfo.getBranch_province(), storeDataInfo.getBranch_city(), storeDataInfo.getBank_card_no(), storeDataInfo.getHolder_mobile(), storeDataInfo.getUnionpay(), storeDataBean.getDoor_pic(),
                    storeDataInfo.getBank_card_front_pic(), storeDataCompany.getLegal_id_card_front_pic(),
                    storeDataCompany.getLegal_id_card_back_pic(), storeDataBean.getLegal_id_card_hand_pic(), storeDataBean.getInside_pic(), storeDataBean.getCashier_desk_pic(),
                    storeDataBean.getSign_pic(), storeDataBasic.getMcc_code(), new DataCallBack<BaseResponseBean>() {
                        @Override
                        public void onSuccessful(BaseResponseBean bean) {
                            dismissProgress();
                            ToastUtil.showShort(bean.getMsg());
                            mActivity.finish();
                        }

                        @Override
                        public void onFailed(String msg, String code) {
                            dismissProgress();
                        }
                    });
        }

    }

    private String TAG = "-------------";

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    List<LocalMedia> mediaList = PictureSelector.obtainMultipleResult(data);
                    if (mediaList.size() > 0) {
                        String imgPath = mediaList.get(0).getCompressPath();
                        requestUploadFile(imgPath);
                    }
                    break;
            }
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.gc();
    }

    /**
     * 上传图片
     */
    public void requestUploadFile(String imgPath) {
        ViewLoading.showProgress(mActivity, "上传中......");
        Bitmap bitmap = BitmapFactory.decodeFile(imgPath);
        String file = Base64BitmapUtil.bitmapToBase64(bitmap);
        homePageModel.requestUploadFile("store", file, new DataCallBack<ImageBean>() {
            @Override
            public void onSuccessful(ImageBean bean) {
                if (isDestroy()) {
                    return;
                }
                ToastUtil.showShort("上传成功");
                if (imgTpye == 1) {
                    imgPath1 = bean.getData().getUrl();
                    GlideUtils.loadImage(mActivity, icPrcture1, imgPath);
                } else if (imgTpye == 2) {
                    imgPath2 = bean.getData().getUrl();
                    GlideUtils.loadImage(mActivity, icPrcture2, imgPath);
                } else if (imgTpye == 3) {
                    imgPath3 = bean.getData().getUrl();
                    GlideUtils.loadImage(mActivity, icPrcture3, imgPath);
                } else if (imgTpye == 7) {
                    imgPath7 = bean.getData().getUrl();
                    GlideUtils.loadImage(mActivity, icPrcture7, imgPath);
                }
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    private void showPictureSelectorDialog() {

        selectPicDialog = new BottomSheetDialog(mActivity, R.style.Dialog);
        selectPicDialog.getWindow().getAttributes().windowAnimations = R.style.commomDialog;
        View view = LayoutInflater.from(mActivity).inflate(R.layout.dialog_bottom_select_pictrue, null);
        // 相册
        TextView album = view.findViewById(R.id.tv_select_pictrue_album);
        // 相机
        TextView camera = view.findViewById(R.id.tv_select_pictrue_camera);
        // 取消
        TextView cancel = view.findViewById(R.id.tv_select_pictrue_cancel);

        album.setOnClickListener(v -> handlerPhotoAlbumClick());
        camera.setOnClickListener(v -> handlerCameraClick());
        cancel.setOnClickListener(v -> cancelPictureDialog());

        selectPicDialog.setContentView(view);
        selectPicDialog.show();
    }

    /**
     * 点击相机
     */
    private void handlerCameraClick() {
        openType = 1;
        permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, //
                Manifest.permission.CAMERA};

        if (PermissionsManager.checkPermissions(permissions)) {
            openCamera();
            cancelPictureDialog();
            return;
        }

        permissionsManager.requestPermissionsEach(permissions);
        permissionsManager.setResultListener((allGranted, rejectPermissionList) -> {
            if (allGranted) {
                openCamera();
                cancelPictureDialog();
            } else {
                permissionsManager.showDialog();
            }
        });
    }

    /**
     * 点击相册
     */
    private void handlerPhotoAlbumClick() {
        openType = 0;
        cancelPictureDialog();
        permissions = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, //
                Manifest.permission.READ_EXTERNAL_STORAGE};

        if (PermissionsManager.checkPermissions(permissions)) {
            openPhotoAlbum();
            cancelPictureDialog();
            return;
        }
        permissionsManager.requestPermissionsEach(permissions);
        permissionsManager.setResultListener((allGranted, rejectPermissionList) -> {
            if (allGranted) {
                openPhotoAlbum();
                cancelPictureDialog();
            } else {
                permissionsManager.showDialog();
            }
        });
    }

    /**
     * 打开相机
     */
    private void openCamera() {
        PictureSelector.create(EBankStoreUpDataFragment.this)
                .openCamera(PictureMimeType.ofImage())
                .loadImageEngine(GlideEngine.createGlideEngine())
                .theme(R.style.picture_default_style)//
                .maxSelectNum(1)//
                .minSelectNum(1)//
                .compress(true)//
                .selectionMedia(mediaList)//
                .cropCompressQuality(30)// 裁剪压缩质量 默认90 int
                .minimumCompressSize(100)// 小于100kb的图片不压缩
                .forResult(PictureConfig.CHOOSE_REQUEST);
        cancelPictureDialog();

    }

    /**
     * 打开相册
     */
    private void openPhotoAlbum() {
        PictureSelector.create(EBankStoreUpDataFragment.this)//
                .openGallery(PictureMimeType.ofImage())
                .loadImageEngine(GlideEngine.createGlideEngine())
                .theme(R.style.picture_default_style)//
                .maxSelectNum(1)//
                .minSelectNum(1)//
                .compress(true)//
                .selectionMedia(mediaList)//
                .cropCompressQuality(30)// 裁剪压缩质量 默认90 int
                .minimumCompressSize(100)// 小于20kb的图片不压缩
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    private void cancelPictureDialog() {
        if (selectPicDialog != null) {
            selectPicDialog.cancel();
        }
    }

    @Override
    public void handlerPermissionForActivityResult() {
        if (PermissionsManager.checkPermissions(permissions)) {
            if (openType == 0) {
                openPhotoAlbum();
                cancelPictureDialog();
            } else if (openType == 1) {
                openCamera();
                cancelPictureDialog();
            }
        }
    }

    private Dialog progress;

    public void showProgress(String message) {
        if (progress == null) {
            progress = new Dialog(mActivity, R.style.ShowProgressUtils);
        }
        View view = View.inflate(mActivity, R.layout.dialog_view_loading, null);
        TextView textView = view.findViewById(R.id.loadingText);
        textView.setText(message);
        progress.setContentView(view);
        progress.setCanceledOnTouchOutside(false);
        progress.setCancelable(true);
        try {
            progress.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void dismissProgress() {
        if (progress == null) {
            return;
        }
        try {
            progress.dismiss();
            progress = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
